#!/usr/bin/env bash

set -e

if [ -z "$SWIFT_VERSION" ]; then
  SWIFT_VERSION_FILE=$(swiftenv-version-file)
  if [ "$SWIFT_VERSION_FILE" == "$SWIFTENV_ROOT/version" ] && ! [[ -f "$SWIFT_VERSION_FILE" ]]; then
    echo "system"
    exit
  fi

  SWIFT_VERSION=$(cat "$SWIFT_VERSION_FILE")
fi

if $DONT_CHECK || swiftenv-prefix "$SWIFT_VERSION" >/dev/null  || [ "$SWIFT_VERSION" == "system" ]; then
  echo "${SWIFT_VERSION##swift-}"
else
  >&2 echo "swiftenv: version \`$SWIFT_VERSION' is not installed"
  exit 1
fi
